#include <stdio.h>
#include <stdlib.h>
 
typedef struct {
	int    size;   // maximum number of elements
	int    start;  // index of oldest element
	int    end;    // index at which to write new element
	float  *elems; // vector of elements
} CircularBuffer;
 
void
cbInit(CircularBuffer *cb, int size)
{
	cb->size  = size + 1; // include empty elem
	cb->start = 0;
	cb->end   = 0;
	cb->elems = calloc(cb->size, sizeof(float));
}
 
void
cbFree(CircularBuffer *cb)
{
	free(cb->elems);
}
 
int
cbIsFull(CircularBuffer *cb)
{
	return (cb->end + 1) % cb->size == cb->start;
}
 
int
cbIsEmpty(CircularBuffer *cb)
{
	return cb->end == cb->start;
}
 
// Write an element, overwriting oldest element if buffer is full. App can
// choose to avoid the overwrite by checking cbIsFull().
void
cbWrite(CircularBuffer *cb, float elem)
{
	cb->elems[cb->end] = elem;
	cb->end = (cb->end + 1) % cb->size;
	if (cb->end == cb->start) {
		cb->start = (cb->start + 1) % cb->size; // full, overwrite
		printf("overflow\n");
	}
}
 
// Read oldest element. App must ensure !cbIsEmpty() first.
float
cbRead(CircularBuffer *cb)
{
	float elem = cb->elems[cb->start];
	cb->start = (cb->start + 1) % cb->size;
	return elem;
}
